package com.example.peibei.ui.activity;

import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;

import com.dingtao.common.core.DataCall;
import com.dingtao.common.core.WDActivity;
import com.dingtao.common.core.exception.ApiException;
import com.dingtao.common.util.SPUtils;
import com.dingtao.common.util.UIUtils;
import com.example.peibei.R;
import com.example.peibei.base.SpConstant;
import com.example.peibei.service.presenter.ChangeLoginPswPresenter;
import com.example.peibei.service.presenter.SetLoginPswPresenter;

import butterknife.BindView;
import butterknife.OnClick;

public class SettingLoginPswActivity extends WDActivity {
    @BindView(R.id.ll_old_psw)
    LinearLayout ll_old_psw;
    @BindView(R.id.et_old_psw)
    EditText et_old_psw;
    @BindView(R.id.et_new_psw)
    EditText et_new_psw;
    @BindView(R.id.et_new_psw_sure)
    EditText et_new_psw_sure;

    private SPUtils spUtils;
    private String password;
    private String token;
    private long mid;
    private SetLoginPswPresenter setLoginPswPresenter;
    private ChangeLoginPswPresenter changeLoginPswPresenter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_setting_login_psw;
    }

    @Override
    protected void initView() {
        spUtils = new SPUtils(SettingLoginPswActivity.this, SpConstant.USER_TOKEN);
        password = spUtils.getString(SpConstant.PASSWORD);
        token = spUtils.getString(SpConstant.USER_TOKEN);
        mid = spUtils.getLong(SpConstant.USER_MID);
        if (password.equals("false")) {
            ll_old_psw.setVisibility(View.GONE);
        } else {
            ll_old_psw.setVisibility(View.VISIBLE);
        }
        setLoginPswPresenter = new SetLoginPswPresenter(new SetLoginPswCall());
        changeLoginPswPresenter = new ChangeLoginPswPresenter(new SetLoginPswCall());
    }

    @OnClick(R.id.iv_back)
    public void back() {
        finish();
    }

    @OnClick(R.id.bt_submit)
    public void submit() {
        if (password.equals("false")) {
            String newPsw = et_new_psw.getText().toString().trim();
            String newPswSure = et_new_psw_sure.getText().toString().trim();
            if (TextUtils.isEmpty(newPsw) || TextUtils.isEmpty(newPswSure)) {
                UIUtils.showToastSafe("新密码不能为空");
                return;
            }
            if (!newPsw.equals(newPswSure)) {
                UIUtils.showToastSafe("两次输入的密码不一致");
                return;
            }
            setLoginPswPresenter.reqeust("Bearer " + token, mid + "", newPsw, "");
        } else {
            String old = et_old_psw.getText().toString().trim();
            String newPsw = et_new_psw.getText().toString().trim();
            String newPswSure = et_new_psw_sure.getText().toString().trim();
            if (TextUtils.isEmpty(old)) {
                UIUtils.showToastSafe("旧密码不能为空");
                return;
            }
            if (TextUtils.isEmpty(newPsw) || TextUtils.isEmpty(newPswSure)) {
                UIUtils.showToastSafe("新密码不能为空");
                return;
            }
            if (newPsw.equals(old)) {
                UIUtils.showToastSafe("新旧密码不能一样");
                return;
            }
            if (!newPsw.equals(newPswSure)) {
                UIUtils.showToastSafe("两次新密码不一致");
                return;
            }
            changeLoginPswPresenter.reqeust("Bearer " + token, mid + "", newPsw, old);
        }
    }

    class SetLoginPswCall implements DataCall<String> {

        @Override
        public void success(String data, Object... args) {
            UIUtils.showToastSafe("设置成功");
            finish();
        }

        @Override
        public void fail(ApiException e, Object... args) {
            UIUtils.showToastSafe(e.getDisplayMessage());
        }
    }

    @Override
    protected void destoryData() {

    }
}
